package com.jingli.pile.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jingli.pile.configuration.PushProperties;
import com.jingli.pile.domain.CommonDataResult;
import com.jingli.pile.domain.StationInfoData;
import com.jingli.pile.entity.PlatformInfo;
import com.jingli.pile.entity.SupStationInfo;
import com.jingli.pile.request.StationQueryRequest;
import com.jingli.pile.request.TokenQueryRequest;
import com.jingli.pile.service.PlatformInfoService;
import com.jingli.pile.service.SupStationInfoService;
import com.jingli.pile.util.HmacMd5Signature;
import com.jingli.pile.util.MessageEncryption;
import com.jingli.pile.util.PileSignUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class StationApiController {
    private Logger logger = LoggerFactory.getLogger(StationApiController.class);
    @Autowired
    private SupStationInfoService stationInfoService;
    @Autowired
    private PushProperties pushProperties;
    @RequestMapping("/evcs/v1/supervise_query_stations_info")
    @ResponseBody
    public CommonDataResult queryStationInfo(@RequestBody TokenQueryRequest queryRequest) throws Exception {
        logger.info("查询站点信息参数:{}",JSONObject.toJSONString(queryRequest));
        String decrypted = MessageEncryption.decrypt(queryRequest.getData(), pushProperties.getDataSecret(), pushProperties.getDataSecretIV());
        logger.info("运营商信息查询参数解密后:{}",decrypted);
        StationQueryRequest stationQueryRequest = JSONObject.parseObject(decrypted, StationQueryRequest.class);
        logger.info("转换后的运营商信息查询结果:{}",JSONObject.toJSONString(stationQueryRequest));
        Page<SupStationInfo> supStationInfoPage = stationInfoService.queryStationInfo(stationQueryRequest);
        StationInfoData infoData = new StationInfoData();
        infoData.setStationInfos(supStationInfoPage.getRecords());
        infoData.setItemSize((int) supStationInfoPage.getTotal());
        infoData.setPageNo((int) supStationInfoPage.getCurrent());
        infoData.setPageCount((int) supStationInfoPage.getPages());
        logger.info("返回的原始站点数据:{}",JSONObject.toJSONString(infoData));
        String signParams = PileSignUtils.signParams(JSONObject.toJSONString(infoData), pushProperties.getDataSecret(), pushProperties.getDataSecretIV());
        String sign = HmacMd5Signature.hmacMd5(signParams,pushProperties.getSignSecret());
        CommonDataResult result = new CommonDataResult();
        result.setRet(0);
        result.setMsg("");
        result.setData(signParams);
        result.setSig(sign);
        return result;
    }
}
